package com.ruoyi.web.controller.vantController.customerController;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.customer.PayableInstalment;
import com.ruoyi.system.service.vantservice.customerervice.ApiLocalFabulousService;
import com.ruoyi.system.service.vantservice.customerervice.ApiPayableInstalmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/instalment")
public class PayableInstalmentController extends BaseController {


    @Autowired
    private ApiPayableInstalmentService payableInstalmentService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询施工队分期支付列表
     */

    @GetMapping("/payable/list")
    public TableDataInfo list(PayableInstalment payableInstalment) {
//        startPage();
        List<PayableInstalment> list = payableInstalmentService.selectPayableInstalmentList(payableInstalment);
        return getDataTable(list);
    }

    @GetMapping("/findAllListById")
    public List<PayableInstalment> findAllListById(PayableInstalment payableInstalment){
        List<PayableInstalment> list = payableInstalmentService.findAllListById(payableInstalment);
        return list;
    }

    @GetMapping(value = "/getnfoById/{instalmentId}")
    public AjaxResult selectPayableInstalmentById(@PathVariable("instalmentId") Long instalmentId)
    {
        return AjaxResult.success(payableInstalmentService.selectPayableInstalmentById(instalmentId));
    }
    /*  */
    @GetMapping(value = "/selectinstalmentState/{instalmentState}")
    public AjaxResult selectinstalmentState(@PathVariable("instalmentState")String instalmentState){
        return AjaxResult.success(payableInstalmentService.selectinstalmentState(instalmentState));
    }

    /**
     * 查询施工队分期支付列表
     */

    @GetMapping("/payable/getList")
    public TableDataInfo getList(PayableInstalment payableInstalment) {
        startPage();
        List<PayableInstalment> list = payableInstalmentService.selectPayableInstalmentList(payableInstalment);
//        Iterator<PayableInstalment> iterator = list.iterator();
//        while (iterator.hasNext()) {
//            PayableInstalment instalment = iterator.next();
//            if (instalment.getVoucherStatus() >= 2) {
//                iterator.remove();
//            }
//        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getVoucherStatus() >=2){
                list.remove(i);
            }
        }
        return getDataTable(list);
    }

    @GetMapping("/selectPayableInstalment")
    public TableDataInfo selectPayableInstalment(PayableInstalment payableInstalment){
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId1 = loginUser.getUser().getUserId();
        payableInstalment.setCustomerId(userId1);
        List<PayableInstalment> payableInstalments = payableInstalmentService.selectPayableInstalment(payableInstalment);
        return getDataTable(payableInstalments);
    }

    @GetMapping("/payable/getVoucherTowList")
    public TableDataInfo selectPayableInstalmentVoucherTowList(PayableInstalment payableInstalment) {
        startPage();
        List<PayableInstalment> list = payableInstalmentService.selectPayableInstalmentVoucherTowList(payableInstalment);
//        Iterator<PayableInstalment> iterator = list.iterator();
//        while (iterator.hasNext()) {
//            PayableInstalment instalment = iterator.next();
//            if (instalment.getVoucherStatus() >= 2) {
//                iterator.remove();
//            }
//        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getVoucherStatus() >=2){
                list.remove(i);
            }
        }
        return getDataTable(list);
    }
    @GetMapping("/payable/getEndAuditList")
    public TableDataInfo selectPayableInstalmentEndAuditList(PayableInstalment payableInstalment) {
        startPage();
        List<PayableInstalment> list = payableInstalmentService.selectPayableInstalmentEndAuditList(payableInstalment);
//        Iterator<PayableInstalment> iterator = list.iterator();
//        while (iterator.hasNext()) {
//            PayableInstalment instalment = iterator.next();
//            if (instalment.getVoucherStatus() >= 2) {
//                iterator.remove();
//            }
//        }
        /*for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getVoucherStatus() >=2){
                list.remove(i);
            }
        }*/
        return getDataTable(list);
    }


    /**
     * 修改凭证审核状态
     *
     * @param payableInstalment
     * @return
     */
    @PutMapping(value = "/payable/editVoucherStatus")
    public AjaxResult editVoucherStatus(@RequestBody PayableInstalment payableInstalment) {
        int i = payableInstalmentService.updatePayableInstalment(payableInstalment);
        if (i == 0) {
            return AjaxResult.error("修改审核状态失败!");
        }
        return AjaxResult.success("修改审核状态成功!");
    }
    /**
     * 获取施工队分期支付详细信息
     */

    @GetMapping(value = "/payable/{instalmentId}")
    public AjaxResult getInfo(@PathVariable("instalmentId") Long instalmentId) {
        PayableInstalment payableInstalment = payableInstalmentService.selectPayableInstalmentById(instalmentId);
        return AjaxResult.success(payableInstalment);
    }

    /**
     * 新增施工队分期支付
     */

    @PostMapping(value = "/payable/add")
    public AjaxResult add(@RequestBody PayableInstalment payableInstalment) {
        return toAjax(payableInstalmentService.insertPayableInstalment(payableInstalment));
    }

    /**
     * 修改施工队分期支付
     */

    @PutMapping(value = "/payable/edit")
    public AjaxResult edit(@RequestBody PayableInstalment payableInstalment) {
        return AjaxResult.success(payableInstalmentService.updatePayableInstalment(payableInstalment));
    }

    @PutMapping(value = "/payable/updateTai")
    public AjaxResult updateTai(@RequestBody PayableInstalment payableInstalment) {
        return AjaxResult.success(payableInstalmentService.updateTai(payableInstalment));
    }

    /**
     * 删除施工队分期支付
     */

    @DeleteMapping("/payable/{instalmentIds}")
    public AjaxResult remove(@PathVariable Long[] instalmentIds) {
        return toAjax(payableInstalmentService.deletePayableInstalmentByIds(instalmentIds));
    }

    @Resource
    private ApiLocalFabulousService apiLocalFabulousService;

    /**
     * 上传支付凭证,并新增施工队分期支付
     *
     * @param file
     * @return
     */
    @PostMapping("/payable/uploadVoucher")
    public Map<String, Object> uploadVoucher(@RequestParam("file") MultipartFile[] file, @RequestParam(value = "instalmentId", required = false) Long instalmentId, @RequestParam(value = "status", required = false) Integer status) {

        Map<String, Object> resultMap = apiLocalFabulousService.uploadPingzheng(file);
        PayableInstalment payableInstalment = new PayableInstalment();
//        payableInstalment.setNumconstructionId(constructionId);

        if (instalmentId != null && instalmentId != 0) {
            payableInstalment.setInstalmentId(instalmentId);
            if (status == 1){
                payableInstalment.setFirstList("http://51jhf.com/profile/" + resultMap.get("img0"));
            }else{
                payableInstalment.setLastList("http://51jhf.com/profile/" + resultMap.get("img0"));
            }
            payableInstalmentService.updatePayableInstalment(payableInstalment);

        } else {
            payableInstalment.setFirstList("http://51jhf.com/profile/" + resultMap.get("img0"));
            payableInstalmentService.insertPayableInstalment(payableInstalment);
            resultMap.put("instalmentId", payableInstalment.getInstalmentId());
        }
        return resultMap;
    }

}
